﻿using gptDataType.Interface;
using gptDataType.Models;
using gptDataType.Type.combination;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace gptDataType.ViewModels
{
    public class PlcVariableViewModel : ReactiveUI.ReactiveObject
    {

        public string Name { get; set; }
        public DataType DataType { get; set; }
        public string Address { get; set; }

        /// <summary>
        /// 动态集合，可以与前端关联
        /// </summary>
        public ObservableCollection<PlcVariableViewModel> Children { get; }
= new ObservableCollection<PlcVariableViewModel>();


        /// <summary>
        /// 此处需要递归添加子元素
        /// </summary>
        /// <param name="member"></param>
        public PlcVariableViewModel(IPlcMember member)
        {

            //InsertChild(member);
            Name = member.Name;
            DataType = member.DataType;
            Address = $"{member.Address.ByteOffset}.{member.Address.BitOffset}";
            if (member is PlcComplexBase pcb)
            {
                foreach (var child in pcb.GetMembers())
                {
                    Children.Add(new PlcVariableViewModel(child));
                }
            }
        }
    }
}
